-----------------------------------------------------------------------------------------------------------------------------------------------
--处理Include和展开macro 分析IF ELSE ENDIF 等预处理，结果还是要放到 globalTableTokenTable里面
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
--全局变量存储全局信息
-----------------------------------------------------------------------------------------------------------------------------------------
--globalTableTokenTable = {}
-----------------------------------------------------------------------------------------------------------------------------------------------
-- 开始 
-----------------------------------------------------------------------------------------------------------------------------------------------
local localIfControlCommandSectionTokenTable = {}

local localStrLuaName="PreParse.lua"
if globalBoolLuaDebugOutputFlag then
	print(localStrLuaName.." start...")
end
-----------------------------------------------------------------------------------------------------------------------------------------
--common tools 
-----------------------------------------------------------------------------------------------------------------------------------------
require(globalStrModuleUtilities)
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
--include Command预处理函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function include_PreParse(tokenCounter,TokenTable,distTable)
	local ret = tokenCounter+1
	local first = TokenTable[ret]
	local includeFileName=""
--第一个Token不存在报错
	if nil == first then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] = "error need ("
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
	end
--第一个Token不是"("的话报错
	if "(" ~= first then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] = "error need ("
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
	end
--第二个Token
	ret = ret +1
	local second = TokenTable[ret]
--第二个Token不存在报错
	if nil == second then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] = " error need )"
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
--第二个Token是")"的话，省略
	elseif ")" == second then
		print(localStrLuaName.." warning: Nothing in "..first.."()")
--第二个Token不是")"的话，添加到表里面
	else
--include文件名字
		includeFileName = second
	end
	ret = ret +1
	local third = TokenTable[ret]
--第三个Token不存在报错
	if nil == third then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] = " error need )"
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
--第三个Token不是")"的话，报错
	elseif ")" ~= third then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] = " error need )"
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
	end
--是否INCLUDE为空
	if "" == includeFileName then
		print(localStrLuaName.." warning: Nothing in "..first.."()")
		return ret
	end
--处理INCLUDE
	local backupGlobalStrLLMakeListsFile = globalStrLLMakeListsFile
	local pathToken = parsePathe2Token(backupGlobalStrLLMakeListsFile)
	local path = table.concat(pathToken,"/",1,table.maxn(pathToken) -1 )
	globalStrLLMakeListsFile = path.."/"..includeFileName
	dofile(globalStrLLMakeHomeScriptPath.."/Token.lua")

	globalStrLLMakeListsFile = backupGlobalStrLLMakeListsFile
	for i,v in pairs(globalTableTokenTable) do
		table.insert(distTable,v)
	end
	
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--include扫描
-----------------------------------------------------------------------------------------------------------------------------------------
function scanIncludeCommand(tokenTable)
	local tokenCounter = 1
	local ret = {}
	while 1 do
		local token = tokenTable[tokenCounter]
		if nil == token then
			break
		end
		local tokenCmp = string.lower(token)
		if "include" == tokenCmp then
			tokenCounter = include_PreParse(tokenCounter,tokenTable,ret)
		else
			tokenCounter = tokenCounter + 1
			table.insert(ret,token)
		end
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--set Command解析函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function setPreParse(tokenCounter,TokenTable,distSetVar,distSetData)
	local tmpDistSetVar ={}
	local tmpDistSetData = {}
	local ret,index = parseCommonHead(tokenCounter,tmpDistSetVar,TokenTable)
	ret = parseCommonTail(ret,tmpDistSetData,TokenTable,index)
	for i,v in pairs(tmpDistSetData) do
		tmpDistSetData[i] = extractVarTable(tmpDistSetData[i],distSetData)
	end
	distSetData[index] = tmpDistSetData[index]
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--set Command扫描函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function scanSetCommand(tokenTable,distSetVar,distSetData)
	local tokenCounter = 1
	local ret = {}
	while 1 do
		local token = tokenTable[tokenCounter]
		if nil == token then
			break
		end
		local tokenCmp = string.lower(token)
		if "set" == tokenCmp then
			tokenCounter = setPreParse(tokenCounter,tokenTable,distSetVar,distSetData)
		else
			tokenCounter = tokenCounter + 1
			table.insert(ret,token)
		end
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--macro Command预处理函数开始
--宏定义如果重复上一次出现的有效
-----------------------------------------------------------------------------------------------------------------------------------------
function macro_PreParse(tokenCounter,TokenTable)
--	macro名称
	local ret,index = parseCommonHead(tokenCounter,globalTableMacroNameTable,TokenTable)
--	macro参数
	ret = parseCommonTail(ret,globalTableMacroARGVTable,TokenTable,index)
--	macro本身
	local tmpMacroToken = {}
	while 1 do
		local token = TokenTable[ret]
		ret = ret + 1
		if nil == token then
			globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
			globalTableErrorHandlerInfo["Resean"] = " error ,macro no end"
			globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
			globalTableErrorHandlerInfo["Position"] = tokenCounter
			dofile(globalStrLLMakeErrorHandler)
		end
		if "endmacro" == token then
			local nextToken_1 = TokenTable[ret]
			local nextToken_2 = TokenTable[ret+1]
			local nextToken_3 = TokenTable[ret+2]
			if nil == nextToken_1 or nil == nextToken_2 or nil == nextToken_3 then
				globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
				globalTableErrorHandlerInfo["Resean"] =  "error ,endmacro no end"
				globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
				globalTableErrorHandlerInfo["Position"] = tokenCounter
				dofile(globalStrLLMakeErrorHandler)
			end
--			print(nextToken_1,nextToken_2,nextToken_3)
			if "(" == nextToken_1 and index == nextToken_2 and ")" == nextToken_3 then
				ret = ret + 3
				break
			else
				globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
				globalTableErrorHandlerInfo["Resean"] =  " error ,endmacro format error"
				globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
				globalTableErrorHandlerInfo["Position"] = tokenCounter
				dofile(globalStrLLMakeErrorHandler)
			end
			
		else
			table.insert(tmpMacroToken,token)
		end
	end
-- 全局变量里面存储
	globalTableMacroTokenTable[index] = tmpMacroToken
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--macro 展开函数开始
--宏定义如果重复上一次出现的有效
-----------------------------------------------------------------------------------------------------------------------------------------
function extractMacro_PreParse(tokenCounter,TokenTable,token,distTable)
--实际参数表
	local macroARGV ={}
--实际参数表里面有，宏参数里面没有的参数
	local macroARGN ={}
--用来替换的Token表
	local tokenReplace = {}
--被替换的Token表
	local tokenToBeReplace = globalTableMacroTokenTable[token]
--宏参数Token表
	local argvList = globalTableMacroARGVTable[token]
	local ret = parseCommonSamilar(tokenCounter,macroARGV,TokenTable)
	local macroARGC = table.maxn(macroARGV)
	tokenReplace["${ARGC}"] = macroARGC
	tokenReplace["${ARGV}"] = macroARGV
	local argvListCounter = table.maxn(argvList)
	if argvListCounter > macroARGC then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] =  "error: macro have not enough arguments"
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
	end
	for i,v in pairs(macroARGV) do
		if i <= argvListCounter then
			local key="${"..argvList[i].."}"
			tokenReplace[key] = v
		else
			table.insert(macroARGN,v)
		end
	end
	tokenReplace["${ARGN}"] = macroARGN
--local set command var to glabal
	local localSetVarTable={}
	local localSetDataTable={}
	local tmpTable = scanSetCommand(tokenToBeReplace,localSetVarTable,localSetDataTable)
	for i,v in pairs(localSetVarTable) do
		local replaceStrSet = token.."@"..table.concat(macroARGV,"@").."@"..v
		tokenReplace[v]= replaceStrSet
		local key= "${"..v.."}"
		tokenReplace[key]= "${"..replaceStrSet.."}"
	end
--Token替换
	local resultTable = commonTokenReplace(tokenToBeReplace,tokenReplace)
--结果代入表
	for i,v in pairs(resultTable) do
		table.insert(distTable,v)
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--macro扫描
-----------------------------------------------------------------------------------------------------------------------------------------
function scanMacroCommand(tokenTable)
	local tokenCounter = 1
	local ret = {}
	while 1 do
		local token = tokenTable[tokenCounter]
		if nil == token then
			break
		end
		local tokenCmp = string.lower(token)
		if "macro" == tokenCmp then
			tokenCounter = macro_PreParse(tokenCounter,tokenTable,ret)
		else
			tokenCounter = tokenCounter + 1
			table.insert(ret,token)
		end
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--扫描展开函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function scanAndExtractMacro(tokenTable)
	local tokenCounter = 1
	local ret = {}
	while 1 do
		local token = tokenTable[tokenCounter]
		if nil == token then
			break
		end
		local flag = findTableMember(token,globalTableMacroNameTable)
		if nil ~= flag then
			tokenCounter = extractMacro_PreParse(tokenCounter,tokenTable,token,ret)
		else
			tokenCounter = tokenCounter + 1
			table.insert(ret,token)
		end
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--if elseif else ifend Command解析函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function judgeExpressionResult(exprTable)
	return true
end
-----------------------------------------------------------------------------------------------------------------------------------------
--if elseif else ifend Command解析函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function ifControlCommandPreParseSection(tokenCounter,TokenTable,parent)
	local expressionTable={}
	local ret = parseCommonSamilar(tokenCounter,expressionTable,TokenTable)
	if 0 == table.maxn(expressionTable) then
		globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
		globalTableErrorHandlerInfo["Resean"] =  " error if command have no expression"
		globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
		globalTableErrorHandlerInfo["Position"] = tokenCounter
		dofile(globalStrLLMakeErrorHandler)
	end
	local exprStr = table.concat(expressionTable,"")
	local sectionLableTrue = parent..exprStr.."=true"
	local sectionLableFalse = parent..exprStr.."=false"
	
	local sectionTableTrue={}
	local sectionTableFalse={}

	local sectionFlag = true
	tokenCounter = ret
	while 1 do
		local token = TokenTable[tokenCounter]
		if nil == token then
			break
		end
		local tokenCmp = string.lower(token)
		if "else" == tokenCmp then
			local endExprTable={}
			local endRet = parseCommonSamilar(tokenCounter,endExprTable,TokenTable)
			tokenCounter = endRet
			if 0 == table.maxn(endExprTable) then
				globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
				globalTableErrorHandlerInfo["Resean"] =  " error endif command have no expression"
				globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
				globalTableErrorHandlerInfo["Position"] = tokenCounter
				dofile(globalStrLLMakeErrorHandler)
			end
			local endExprStr=table.concat(endExprTable,"")
			if endExprStr == exprStr then
				sectionFlag = false
			end
		elseif "endif" == tokenCmp then
			local endExprTable={}
			local endRet = parseCommonSamilar(tokenCounter,endExprTable,TokenTable)
			tokenCounter = endRet
			if 0 == table.maxn(endExprTable) then
				globalTableErrorHandlerInfo["luaFile"] = localStrLuaName
				globalTableErrorHandlerInfo["Resean"] =  "error endif command have no expression"
				globalTableErrorHandlerInfo["Token"] = TokenTable[tokenCounter]
				globalTableErrorHandlerInfo["Position"] = tokenCounter
				dofile(globalStrLLMakeErrorHandler)
			end
			local endExprStr=table.concat(endExprTable,"")
			if endExprStr == exprStr then
				break
			end
		elseif "elseif" == tokenCmp or "if" == tokenCmp then
			local currentCondition;
			if true == sectionFlag then
				currentCondition = sectionLableTrue
			else
				currentCondition = sectionLableFalse
			end
			tokenCounter = ifControlCommandPreParseSection(tokenCounter,TokenTable,sectionLableTrue)
		else
			if true == sectionFlag then
				table.insert(sectionTableTrue,token)
			else
				table.insert(sectionTableFalse,token)
			end
			tokenCounter = tokenCounter + 1
		end
	end
	localIfControlCommandSectionTokenTable[sectionLableTrue] = sectionTableTrue
	localIfControlCommandSectionTokenTable[sectionLableFalse] = sectionTableFalse
	ret = tokenCounter
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--if elseif else ifend Command扫描函数开始
-----------------------------------------------------------------------------------------------------------------------------------------
function scanIfControlCommandPreParse(TokenTable,parentExpr)

	local ret = {}
	local tokenCounter = 1
	while 1 do
		local token = TokenTable[tokenCounter]
		if nil == token then
			break
		end
		local tokenCmp = string.lower(token)
		if "if" == tokenCmp then
			tokenCounter = ifControlCommandPreParseSection(tokenCounter,TokenTable,parentExpr)
		else
			tokenCounter = tokenCounter + 1
			table.insert(ret,token)
		end
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
if globalBoolCMakeFlage then
	dofile(globalStrLLMakeHomeScriptPath.."/PreParseCMake.lua")
end
local FirstTokenTable = globalTableTokenTable
-----------------------------------------------------------------------------------------------------------------------------------------
--保存到LastTokenTable里面
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = scanIncludeCommand(FirstTokenTable)
-----------------------------------------------------------------------------------------------------------------------------------------
--if globalBoolLuaDebugOutputFlag then
--	print("Include scan Token Table")
--	for i,v in pairs (FirstTokenTable) do 
--		print(i,v)
--	end
--	print("---------------end")
--end
-----------------------------------------------------------------------------------------------------------------------------------------
--扫描macro定义开始
--保存到FirstTokenTable里面
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = scanMacroCommand(FirstTokenTable)
--if globalBoolLuaDebugOutputFlag then
--	print(" Macro Scan Token Table")
--	for i,v in pairs (FirstTokenTable) do 
--		print(i,v)
--	end
--	print("---------------end")
--end
-----------------------------------------------------------------------------------------------------------------------------------------
--展开宏
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = scanAndExtractMacro(FirstTokenTable)
--if globalBoolLuaDebugOutputFlag then
--	print(" Macro extra Token Table")
--	for i,v in pairs (FirstTokenTable) do 
--		print(i,v)
--	end
--	print("---------------end")
--end
-----------------------------------------------------------------------------------------------------------------------------------------
--set comand extra
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = scanSetCommand(FirstTokenTable,globalTableSetVarTable,globalTableSetDataTable)
if globalBoolLuaDebugOutputFlag then
	print(" Macro extra Token Table")
	for i,v in pairs (FirstTokenTable) do 
		print(i,v)
	end
	print("---------------end")
end
-----------------------------------------------------------------------------------------------------------------------------------------
--if elseif else ifend comand
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = scanIfControlCommandPreParse(FirstTokenTable,"true=true")
-----------------------------------------------------------------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------------------------------------------------------------
FirstTokenTable = extractVarTable(FirstTokenTable,globalTableSetDataTable)
-----------------------------------------------------------------------------------------------------------------------------------------
--代入全局变量
-----------------------------------------------------------------------------------------------------------------------------------------
globalTableTokenTable = FirstTokenTable
-----------------------------------------------------------------------------------------------------------------------------------------
--打印Token
-----------------------------------------------------------------------------------------------------------------------------------------
if globalBoolLuaDebugOutputFlag then
	for i,v in pairs (globalTableTokenTable) do 
		print(i,v)
	end
end
if globalBoolLuaDebugOutputFlag then
	print(localStrLuaName.." ...end")
end

